<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=     , initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    let arr = [1, [2, [3, [4, 5]]], 6];
  arr_flat = arr.flat(Infinity);
  console.log(arr_flat)
    function flat2 (arr) {
      const result = []
      const fn = function(ary){
        ary.forEach(item => {
          if(item instanceof Array){
            fn(item)
          }
          else{
            result.push(item)
          }
        });
      }
      fn(arr)
      return result
    }
    function    flat3 (arr) {
      return arr.reduce((result, item) => {
        return result.concat(Array.isArray(item) ? this.flat3(item) : item)
      }, [])
    }
    function flat5(arr){
      return arr.reduce((result,item)=>{
        return result.concat(Array.isArray(item) ? this.flat5(item) : item)
      },[])
    }
    function flat4(arr){
      while(arr.some(Array.isArray)){
        console.log(arr)
        arr=[].concat(...arr)
      }
      return arr
    }
    console.log(flat2(arr))
    console.log(flat4(arr))
  </script>
</body>
</html>